package com.dz.Incubator.service.impl;

import com.dz.Incubator.dao.BannerDao;
import com.dz.Incubator.entity.BannerEntity;
import com.dz.Incubator.service.BannerService;
import com.dz.Incubator.config.Pagination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BannerServiceImpl implements BannerService {

    private static final Logger logger = LoggerFactory.getLogger(BannerService.class);

    @Autowired
    private BannerDao bannerDao;

    @Override
    public BannerEntity insert(BannerEntity bannerEntity) {
        bannerDao.insert(bannerEntity);

        return bannerEntity;
    }

    @Override
    public BannerEntity update(BannerEntity bannerEntity) {
        bannerDao.update(bannerEntity);

        return bannerEntity;
    }

    @Override
    public Integer delete(Long id) {
        int ret = bannerDao.delete(id);
        return ret;
    }

    @Override
    public List<BannerEntity> find(Long[] ids) {
        List<BannerEntity> datas = bannerDao.find(ids);
        return datas;
    }

    @Override
    public BannerEntity get(Long id) {
        BannerEntity bannerEntity = bannerDao.get(id);
        return bannerEntity;
    }

    @Override
    public List<BannerEntity> findAll(int appType) {
        List<BannerEntity> datas = bannerDao.findAll();
        //appType -1 返回所有数据
        if(-1 == appType){
            return datas;
        }
        //appType 不是-1 返回对应类型数据
        List<BannerEntity> datasList = new ArrayList<>();
        for(BannerEntity bannerEntity:datas){
            if(bannerEntity.getAppType() == appType){
                datasList.add(bannerEntity);
            }
        }
        return datasList;
    }

    @Override
    public Pagination findByPagination(Pagination pagination) {
        List<BannerEntity> datas = bannerDao.findByPagination(pagination);
        pagination.setList(datas);
        return pagination;
    }

    @Override
    public List<BannerEntity> insertBatch(List<BannerEntity> list) {
        int ret = bannerDao.insertBatch(list);
        return list;
    }

    @Override
    public List<BannerEntity> updateBatch(List<BannerEntity> list) {
        int ret = bannerDao.updateBatch(list);
        return list;
    }

    @Override
    public List<Long> deleteBatch(Long[] ids) {
        int ret = bannerDao.deleteBatch(ids);
        List<BannerEntity> datas = bannerDao.find(ids);
        List<Long> list = new ArrayList<Long>();
        for (BannerEntity data : datas) {
            list.add(data.getId());
        }
        return list;
    }
}